//
//  GKWelcomeViewController.swift
//  MicroBlogFunc
//
//  Created by GK on 16/4/11.
//  Copyright © 2016年 GK. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class GKWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.prepareUI()
        self.setIcon()
        
        //网络请求用户信息
        GKUserAccountViewModel.shareInstance.loadUserInfo { (error) in
            if error != nil
            {
                GKPrint("请求用户信息失败\(error)")
                return
            }
            self.setIcon()//此时请求用户信息成功
        }
    }
    
     
    //网络请求用户头像
    private func setIcon()
    {
        if let avatar_large = GKUserAccountViewModel.shareInstance.userAccount?.avatar_large
        {
            let IconURL = NSURL(string: avatar_large)
            self.headView.sd_setImageWithURL(IconURL, placeholderImage: UIImage(named: "avatar_default_big"))
        }
    }
    
    //更新UI
    private func prepareUI() {
        
        view.addSubview(bgView)
        view.addSubview(headView)
        view.addSubview(lblText)
        
        bgView.translatesAutoresizingMaskIntoConstraints = false
        headView.translatesAutoresizingMaskIntoConstraints = false
        lblText.translatesAutoresizingMaskIntoConstraints = false
        
        //添加约束
        bgView.snp_makeConstraints { (make) in
            make.edges.equalTo(EdgeInsetsZero)
        }
        headView.snp_makeConstraints { (make) in
            make.centerX.equalTo(view)
            make.bottom.equalTo(view).offset(-160)
        }
        lblText.snp_makeConstraints { (make) in
            make.centerX.equalTo(view)
            make.top.equalTo(headView.snp_bottom).offset(16)
        }
        
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimating()
    }
    
    //开始界面的动画
    private func startAnimating(){
        
        headView.snp_updateConstraints { (make) in
            make.bottom.equalTo(view).offset( -(UIScreen.mainScreen().bounds.height - 160))
        }
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: {
            //通知系统来重新布局
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
                UIView.animateWithDuration(0.25, animations: { 
                    
                    self.lblText.alpha = 1
                    }, completion: { (_) -> Void in
                        let vc = GKTabBarController()
                        (UIApplication.sharedApplication().delegate as! AppDelegate).switchViewController(vc)
                })
        }
    }
    
    
    //懒加载
    
    //背景图
    private lazy var bgView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    //头像
    private lazy var headView: UIImageView = {
        
        let hdView = UIImageView(image: UIImage(named: "avatar_default_big"))
        hdView.layer.cornerRadius = 42.5
        hdView.layer.masksToBounds = true
        return hdView
    }()
    //文字label
    private lazy var lblText: UILabel = {
       
        let lbl = UILabel()
        lbl.text = "欢迎归来"
        lbl.textColor = UIColor.blackColor()
        lbl.font = UIFont.systemFontOfSize(18)
        lbl.sizeToFit()
        lbl.alpha = 0
        return lbl
    }()
    
}
